﻿Imports System.Data.SqlClient

Public Class RestauranteDAO

    Function buscar(ByVal modelo As Restaurante) As Collection

        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "select nombre, tipo_comida, distrito, direccion from Restaurante where nombre like @VALOR1 and tipo_comida like @VALOR2 and distrito like @VALOR3"
            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", "%" + modelo.nombre + "%")
            cmd.Parameters.AddWithValue("@VALOR2", "%" + modelo.tipo_comida + "%")
            cmd.Parameters.AddWithValue("@VALOR3", "%" + modelo.distrito + "%")

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()

            While dr.Read
                Dim model As New Restaurante
                model.nombre = dr(0).ToString
                model.tipo_comida = dr(1).ToString
                model.distrito = dr(2).ToString
                model.direccion = dr(3).ToString
                lista.Add(model)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista


    End Function

    Function buscartodo(ByVal modelo As Restaurante) As Collection

        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "select * from Restaurante where nombre like @VALOR1 and tipo_comida like @VALOR2 and distrito like @VALOR3 and direccion like @VALOR4 and ruc like @VALOR5"
            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", "%" + modelo.nombre + "%")
            cmd.Parameters.AddWithValue("@VALOR2", "%" + modelo.tipo_comida + "%")
            cmd.Parameters.AddWithValue("@VALOR3", "%" + modelo.distrito + "%")
            cmd.Parameters.AddWithValue("@VALOR4", "%" + modelo.direccion + "%")
            cmd.Parameters.AddWithValue("@VALOR5", "%" + modelo.ruc + "%")

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()

            While dr.Read
                Dim model As New Restaurante
                model.idrestaurant = dr(0).ToString
                model.nombre = dr(1).ToString
                model.tipo_comida = dr(2).ToString
                model.distrito = dr(3).ToString
                model.direccion = dr(4).ToString
                model.ruc = dr(5).ToString
                lista.Add(model)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista


    End Function

    Public Function listar() As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT * FROM Restaurante"
            Dim cmd As New SqlCommand(sql, cn)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            While dr.Read
                Dim modelo As New Restaurante
                modelo.idrestaurant = dr(0).ToString
                modelo.nombre = dr(1).ToString
                modelo.tipo_comida = dr(2).ToString
                modelo.distrito = dr(3).ToString
                modelo.direccion = dr(4).ToString
                modelo.ruc = dr(5).ToString
                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function

    Public Function insertar(ByVal modelo As Restaurante) As Restaurante
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "INSERT INTO Restaurante (NOMBRE, tipo_comida, distrito, direccion, ruc) VALUES (@VALOR1, @VALOR2, @VALOR3, @VALOR4, @VALOR5)"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.nombre)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.tipo_comida)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.distrito)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.direccion)
            cmd.Parameters.AddWithValue("@VALOR5", modelo.ruc)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No insertó")
            End If

            ' LEER EL ULTIMO ID
            sql = "SELECT @@IDENTITY"
            cmd = New SqlCommand(sql, cn)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idrestaurant = CType(dr(0), Integer)
            End If

            dr.Close()
            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo

    End Function

    Public Sub actualizar(ByVal modelo As Restaurante)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "UPDATE restaurante SET nombre=@VALOR1, tipo_comida=@VALOR2, distrito=@VALOR3, direccion=@VALOR4, ruc=@VALOR5 WHERE idrestaurant=@VALOR6"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.nombre)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.tipo_comida)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.distrito)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.direccion)
            cmd.Parameters.AddWithValue("@VALOR5", modelo.ruc)
            cmd.Parameters.AddWithValue("@VALOR6", modelo.idrestaurant)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No actualizó")
            End If

            cn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub

    Public Sub actualizarxname(ByVal modelo As Restaurante)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "UPDATE restaurante SET nombre=@VALOR1, tipo_comida=@VALOR2, distrito=@VALOR3, direccion=@VALOR4, ruc=@VALOR5 WHERE nombre like @VALOR1"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.nombre)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.tipo_comida)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.distrito)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.direccion)
            cmd.Parameters.AddWithValue("@VALOR5", modelo.ruc)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No actualizó")
            End If

            cn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub

    Public Sub eliminar(ByVal idrestaurant As Integer)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "DELETE FROM restaurante WHERE idrestaurant=@VALOR1"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", idrestaurant)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No eliminó")
            End If

            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub

    Public Function obtener(ByVal idCategoria As Integer) As Restaurante
        Dim modelo As New Restaurante

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT * from restaurante WHERE idrestaurant=@VALOR1"
            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR1", idCategoria)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idrestaurant = dr(0).ToString
                modelo.nombre = dr(1).ToString
                modelo.tipo_comida = dr(2).ToString
                modelo.distrito = dr(3).ToString
                modelo.direccion = dr(4).ToString
                modelo.ruc = dr(5).ToString
            End If

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo
    End Function

    Public Function obtener2(ByVal nombre As String) As Restaurante
        Dim modelo As New Restaurante

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT * from restaurante WHERE nombre like @VALOR1"
            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR1", "%" + nombre + "%")

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idrestaurant = dr(0).ToString
                modelo.nombre = dr(1).ToString
                modelo.tipo_comida = dr(2).ToString
                modelo.distrito = dr(3).ToString
                modelo.direccion = dr(4).ToString
                modelo.ruc = dr(5).ToString
            End If

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo
    End Function

End Class
